﻿// tut_b1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <osgViewer/viewer>
#include <osgEarth/Notify>
#include <osgEarth/EarthManipulator>
#include <osgEarth/MapNode>
#include <osgEarth/Threading>
#include <osgEarth/ShaderGenerator>
#include <osgDB/ReadFile>
#include <osgGA/TrackballManipulator>
#include <osgUtil/Optimizer>
#include <iostream>
#include <osgEarth/Metrics>
#include "osgEarth/GDAL";
using namespace osgEarth;
using namespace osgEarth::Util;
#include <iostream>
using namespace std;

//IMGUI
#include <imgui.h>
#include <imgui_impl_opengl3.h>
#include "OsgImGuiHandler.hpp"
//初始化IMGUI
class ImGuiInitOperation : public osg::Operation
{
public:
	ImGuiInitOperation()
		: osg::Operation("ImGuiInitOperation", false)
	{
	}
	void operator()(osg::Object* object) override
	{
		osg::GraphicsContext* context = dynamic_cast<osg::GraphicsContext*>(object);
		if (!context)
			return;
		if (!ImGui_ImplOpenGL3_Init())
		{
			std::cout << "ImGui_ImplOpenGL3_Init() failed\n";
		}
	}
};
//IMGUI控件绘制类
class MyIMGuiHandler : public OsgImGuiHandler
{
public:
	MyIMGuiHandler() {
		_inputBuff[0] = '\0';
	}
protected:
	void drawUi() override //绘制控件
	{
		// ImGui code goes here...
		ImGui::Begin("Panel Title");
		ImGui::Text("dear imgui says hello! (%s) (%d)", IMGUI_VERSION, IMGUI_VERSION_NUM);
		ImGui::Spacing();
		if (ImGui::Button("click"))
		{
			cout << "A button clicked" << endl;
		}
		ImGui::InputText("input control", _inputBuff, 127 );
		ImGui::SliderFloat("slider control", &_fvalue, 0.0f, 1.0f);
		ImGui::ColorEdit4("Color", _color );
		ImGui::End();
	}
private:
	float _fvalue =0;
	char _inputBuff[128];
	float _color[4] = { 1,1,1,1 };
};

//1. download hpp cpp files in https://github.com/Tordan/imgui-osg/tree/master/src
int main()
{
	osgEarth::initialize();
	//create a viewer
	osgViewer::Viewer viewer;
	viewer.setUpViewInWindow(0, 100, 800, 600);
	viewer.setReleaseContextAtEndOfFrameHint(false);
	//set camera manipulator
	viewer.setCameraManipulator(new EarthManipulator);
	osgEarth::Map* map = new osgEarth::Map();
	//layer
	GDALImageLayer* layer1 = new GDALImageLayer();
	layer1->setURL(osgEarth::URI("E:/coding/osgearth2023/osgearth3.4/data/world.tif"));
	map->addLayer(layer1);
	osg::ref_ptr<osgEarth::MapNode> mapNode = new osgEarth::MapNode(map);
	viewer.setSceneData(mapNode);
	//IMGUI
	viewer.setRealizeOperation(new ImGuiInitOperation);
	viewer.addEventHandler(new MyIMGuiHandler);
	
	return Metrics::run(viewer);
}